var app = getApp();
import ajaxhelper from '../../utils/ajaxhelper.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList:[],
    isChecked: false,
    text:'支付' ,
    time:'',
    name:'',
    totalPrice:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    console.log(option)
    console.log(option.id)
    this.getListData(option.id); //获取列表数据
  },

  //列表渲染数据
  getListData: function (id) {
    let param = {}
    var token = (wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token) || '';
    token = encodeURIComponent(token);
    ajaxhelper.post(app.globalData.frontJSHost + "priceResourcePay/detail/" + id+"?token="+token, param, this, this.setListData)
  },
  setListData(res){
    console.log(res)
    if(res.status == 200){

      var Data = res.data;
      var gmtCreate = Data.gmtCreate;
      var time = new Date(gmtCreate).format("yyyy-MM-dd hh:mm:ss");
      var totalPrice = Data.price;
      var name = Data.sourceName
      console.log(Data)
      if(Data){
        this.setData({
          dataList: this.data.dataList.concat(Data),
          time:time,
          totalPrice: totalPrice,
          name: name
        })
      }
    }
  },
  // 去支付
  goPay:function(event){
    var id = event.currentTarget.dataset.id;
    var name = event.currentTarget.dataset.sourceName;
    var totalPrice = event.currentTarget.dataset.price;
    var mediaType = event.currentTarget.dataset.mediaType;
    var sourceId = event.currentTarget.dataset.sourceId;
    console.log(id)
    var that=this;
    this.setData({
      isChecked: true,
      text:'努力加载中，请稍后...'
    })
    var params={
      id:id
    } 
    var token = wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token;
    ajaxhelper.post(app.globalData.frontJSHost + 'priceResourcePay/v1/payOrder?token=' + encodeURIComponent(token), params, this,function(res) {
      console.log(res)
      if (res.status && res.bizCode == 200) {
        if (res.data.data && !res.data.data.params.hasOwnProperty('errorCode')) {
          var payData = res.data.data.params;
          let nonceStr = payData.nonceStr;
          let timeStamp = payData.timeStamp;
          let packageValue = payData.package;
          let signPay = payData.signType;
          let paySign = payData.paySign;
          wx.requestPayment({
            timeStamp: timeStamp,
            nonceStr: nonceStr,
            package: packageValue,
            signType: signPay,
            paySign: paySign,
            success: function (res) {
              console.log(res);
              // 支付成功后关闭音乐，由于有试听地址的原因
              // var backgroundAudioManager = app.backgroundAudioManager;
              // if (backgroundAudioManager.src) {
              //   backgroundAudioManager.stop();
              // }
              // 获取全局音频信息
              var urlObj = wx.getStorageSync('gloalObjUrl');
              urlObj = urlObj ? JSON.parse(urlObj) : {};
              if (urlObj.courseId == sourceId) {
                console.log('购买了当前的' + sourceId);
                urlObj.canView = true;
                wx.setStorage({
                  key: 'gloalObjUrl',
                  data: JSON.stringify(urlObj),
                })
              }
              /////
              wx.redirectTo({
                url: '../successPurchase/successPurchase?name=' + name + '&price=' + totalPrice + '&id=' + id + '&mediaType=' + mediaType + '&sourceId=' + sourceId,
              })
            },
            fail: function (res) {
              that.setData({
                isChecked: false,
                text: '支付'
              })
              wx.showModal({
                title: '支付失败',
                content: '',
                showCancel: false,
                confirmText: '确定',
                confirmColor: '#FF4612',
                success: function (res) {
                  
                }
              })
            }
          })
        } else {
          wx.showToast({
            title: !!res.data.data.params.errorCodeDesc ? res.data.data.params.errorCodeDesc : "订单出错",
            icon: "loading"
          })
        }
      } else {
        that.setData({
          isChecked: false,
          text: '支付'
        })
        wx.showToast({
          title: !!res.msg ? res.msg : "请求出错",
          icon: "loading"
        })
      }
    })
  },

  //去专辑详情
  goDetail: function (event){
    var id = event.currentTarget.dataset.id;
    var mediaType = event.currentTarget.dataset.mediaType;
    if (mediaType == "AUDIO"){
      wx.navigateTo({
        url: '../audioAlbumDetail/audioAlbumDetail?id=' + id
      })
    }else{
      wx.navigateTo({
        url: '../videoAlbumDetail/videoAlbumDetail?id=' + id
      })
    }
  }
})